<?php
/**
* @authors darl2ng@gmail.com, ngocanh.ngo@gmail.com
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Call {
	
	var $__ID;
	var $__Type;
	var $__Value;
	var $__Parameters; //MUST have only one dimension (values) so that call_user_func_array works as expected
		
	public function __construct($i,$xmlcall) {
		$this->__ID=$i;
		$this->parseCall($xmlcall);
	}
	
	public function execute(&$response) {//passing reference so that $response will be updated
		$method=$this->__Value;
		if ($this->validate()) { 
			switch ($this->__Type) {
				case 'xajax':
					if (method_exists($response,$method)) { 
						//check if the xajaxResponse method name is correct, then call it with parameters in array
						call_user_func_array(array($response,$method),$this->__Parameters);
					} else {
						$response->alert('function not available');
					}			
					break;
				case 'dypax':
					$response->alert($method.':'.implode(',',$this->__Parameters)); // do something else
					//TODO: add mediator to this call to execute the desired operation (php)
					//For now, only alert
					break;
				default:
					$response->alert('type not supported');
					break;
			}		
		} else {
			$response->alert('call not validated');
		}
	}
	public function getId() {
		return $this->__ID;
	}
	public function getType() {
		return $this->__Type;		
	}	
	public function getValue() {
		return $this->__Value;		
	}
	public function getParameters() {
		return $this->__Parameters;
	}
	public function getNumberParameters() {
		return count($this->__Parameters);
	}	
	public function getParameterValueById($i) {
		return $this->__Parameters[$i];
	}
	public function toString() {
		$ret=$this->__ID;				
		$ret.=$this->__Value;
		foreach($this->__Parameters as $param){
			$ret.=$param['value'];			
		}							
		return $ret;	
	}
	public function validate() {		
		
		if ($this->__Type =='dypax') return true;
		
		$isValidated=false;
		
		switch ($this->__Value) {
			case 'assign':
				if (count($this->__Parameters)==3) $isValidated=true; 					
				break;
			case 'preprend':
				if (count($this->__Parameters)==3) $isValidated=true;					
				break;
			case 'clear':
				if (count($this->__Parameters)==2) $isValidated=true;
				break;
			case 'remove':
				if (count($this->__Parameters)==1) $isValidated=true;
				break;					
			case 'alert':
				if (count($this->__Parameters)==1) $isValidated=true;
				break;	
			case 'redirect':
				if (count($this->__Parameters)==1) $isValidated=true;
				break;
			case 'script':
				if (count($this->__Parameters)==1) $isValidated=true;
				break;								
			case 'call':
				$isValidated=true;
				break;
			default:
				break;
		}
		return $isValidated;
	}
	private function parseCall($xmlcall) {
		$this->__Value = (string) $xmlcall['value'];
		$this->__Type = (string) $xmlcall['type'];		
		$this->__Parameters = array();
		foreach($xmlcall->param as $param){
			$this->__Parameters[] = (string) $param['value'];
		}				
	}	
}
?>